<?php
namespace Home\Controller;
use Think\Controller;
class BzjpbmfController extends Controller {
    public function index(){
        indexRvent();//测算首页执行事件
        $this->assign('titleName',titleName('Bzjpbmf',1));//标题名称

        if($_REQUEST['channel']!=''){
            cookie('channel',$_REQUEST['channel']);//获取渠道号
        }

        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            getToken();
            getTicket();

            $wzurl=wxJsdkData();
            if(!empty($_REQUEST['code'])){
                cookie('code',$_REQUEST['code'],7100);
            }
            getCode($wzurl);
            getOopenid();
            cookie('uid',cookie('openid'));
        }

        if(!empty($_REQUEST['uid'])){
            cookie('uid',$_REQUEST['uid']);
        }else{
            $imei=cookie('imei');//生成唯一标识imei
            $uid=cookie('uid');
            if(empty($imei)){$imei=date('Ymd').mt_rand(10000,99999).date('His');cookie('imei',$imei);}
            if(empty($uid)){cookie('uid',$imei);}
        }

        $this->display("Bzjp_bmf/bzjp_bmf");
    }
    public function bzjp_bmf_lq(){
        $this->display("Bzjp_bmf/bzjp_bmf_lq");
    }
    public function bzjp_bmf_zhifu(){
        $this->assign('titleName',titleName('Bzjpbmf',2));//标题名称
        $strData=mb_substr(cookie('orderid'),0,2);
        if($strData!='JP'){cookie('orderid',null);}

        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
        }

        if($_REQUEST['birthday']==''){
            $birthday='0-2018-08-08-8';
        }else{
            $birthday=$_REQUEST['birthday'];
        }

        if($_REQUEST['username']){//返回操作
            cookie('znickname',I("request.username"));
            cookie('zymd',mb_substr($birthday,2,10));
            cookie('zhour',mb_substr($birthday,13,2));
            cookie('datetype',mb_substr($birthday,0,1));
            cookie('zsex',$_REQUEST['gender']);
            cookie('area',$_REQUEST['area']);
            cookie('areaes',I("request.areaes"));
            cookie('phone',$_REQUEST['phone']);
        }
        if(!empty($_REQUEST['ordernum'])){//未支付订单
            $arrdata=M()->query("select username,datetype,year,month,day,hour,sex from sm_h5test where ordernum='{$_REQUEST['ordernum']}' limit 1");
            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd);
            cookie('zhour',$arrdata[0]['hour']);
            cookie('znickname',$arrdata[0]['username']);
            cookie('zsex',$arrdata[0]['sex']);
            cookie('datetype',$arrdata[0]['datetype']);
            cookie('orderid',$_REQUEST['ordernum']);
        }

        $data=cookie('orderid');//获取订单号
        if(empty($data)&&empty($_REQUEST['ordernum'])){
            $orderid='JPBM'.date('Ymd').mt_rand(1000,9999).date('His');
            cookie('orderid',$orderid);
        }

        //创建订单
        $hour=cookie('zhour');
        $shop=cookie('orderid');
        $znickname=cookie('znickname');if($znickname=='')$znickname='匿名';
        $zsex=cookie('zsex');if($zsex=='')$zsex=1;
        $channel=cookie('channel');
        $imei=cookie('uid');if(cookie('uid')==''){$imei=cookie('imei');}
        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $datetype=cookie('datetype');if($datetype=='')$datetype=0;
        $area=cookie('area');
        $areaes=cookie('areaes');
        $phone=cookie('phone');
        cookie('price',68);//价格

        $channelPayArr=M()->query("select pay from tb_channel_pay where channel='{$channel}' limit 1");//查询渠道价格
        if($channelPayArr){cookie('price',$channelPayArr[0]['pay']);}

        $datastr=stristr(cookie('znickname'),'测试');
        if($datastr){cookie('price',0.01);}

        $price=cookie('price');
        if(empty($_REQUEST['ordernum'])){
            $arrdata=M()->query("select id from sm_h5test where ordernum='{$shop}' limit 1");
            if(empty($arrdata)){
                M()->query("insert into sm_h5test (ordernum,price,username,typeid,sex,status,datetype,year,month,day,hour,paykind,appuserid,remark,areaes,phone,ip,channel,auto_send_sms)values(
                                              '{$shop}','{$price}','{$znickname}',31,{$zsex},-1,'{$datetype}',{$y},{$m},{$d},{$hour},-1,'{$imei}','{$area}','{$areaes}','{$phone}','{$_SERVER['REMOTE_ADDR']}','{$channel}',0)");
            }
            if(!cookie($_REQUEST['username'])){//返回操作
                //更新订单
                M()->query("update sm_h5test set username='{$znickname}',datetype='{$datetype}',year={$y},month={$m},day={$d},sex={$zsex},remark='{$area}',areaes='{$areaes}',phone='{$phone}' where ordernum='{$shop}'");
            }
        }

        if(cookie('zsex')==1){$zsex='男';}else{$zsex='女';}
        zwmp(cookie('zymd'), cookie('zhour'),$zsex,'sizhu');
        $sizhu=I('globals.sizhu');
        $this->assign('sizhu',$sizhu);//Z四柱



        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
        }else{
            $xml=wxgzhzf('JPBM','八字本命佛',cookie('orderid'));//公众号支付回调参数
            $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $val = json_decode(json_encode($xmlstring),true);
            $noncestr=noncestr(15);//随机字符串
            $timestamp=time();//时间戳

            $stringA="appId=".cookie('APPID')."&nonceStr={$noncestr}&package=prepay_id={$val['prepay_id']}&signType=MD5&timeStamp={$timestamp}";
            $stringSignTemp=$stringA."&key=".cookie('wxKEY');
            $sign=strtoupper(md5($stringSignTemp));

            $this->assign('timeStamp',$timestamp);
            $this->assign('nonceStr',$noncestr);
            $this->assign('package','prepay_id='.$val['prepay_id']);
            $this->assign('signType','MD5');
            $this->assign('paySign',$sign);
            $this->assign('appId',cookie('APPID'));

        }

        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $zyinli=yinli($y,$m,$d);
        cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时');//阳历
        cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2]." ".mb_substr($sizhu[3],1,1,'utf-8')."时");//阴历

        if($datetype==1){
            $this->assign('datetype',"农历 ".cookie('zyinli'));
        }else{
            $this->assign('datetype',"阳历 ".cookie('zyangli'));
        }


        $this->assign('znickname',cookie('znickname'));
        $this->assign('channel',cookie('channel'));
        $this->assign('wxlogin',cookie('wxlogin'));
        $this->assign('orderid',cookie('orderid'));

        $this->display("Bzjp_bmf/bzjp_bmf_zhifu");
    }

    public function pay(){
        $this->assign('ordername1','本命佛');
        $this->assign('ordername2','知命-本命佛');
        $this->display("Index/pay");
    }
    public function paywx(){
        H5ZMwxzhifu('JPBM','本命佛',cookie('orderid'),'Bzjpbmf');//知命
    }

}